package com.qcs.ando.memory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

import com.qcs.ando.BaseActivity;
import com.qcs.ando.manage.ImageCacheManager;
import com.qcs.ando.manage.ImageManager;

public class SurfaceActivity extends BaseActivity {
    /** �߳�����ʱ�� */
    private static final int SLEEP_TIME = 100;
    /** ͼƬ���ڴ������ŵı��� */
    private static final int MEMORY_FRACTION = 1;

    /** ��Ҫ���ص�ͼƬ */
    private static final String[] imgs = {
            "memory_hpng/artist_bg.png",
            "memory_hpng/background02.png",
            "memory_hpng/background03.png",
            "memory_hpng/bg01.png",
            "memory_hpng/bg02.png",
            "memory_hpng/bg03.png",
            "memory_hpng/bg04.png",
            "memory_hpng/big_map_bg.png",
            "memory_hpng/cankao.png",
            "memory_hpng/cartoon_bg.png",
            "memory_hpng/daily_course_bg.png",
            "memory_hpng/map_island_main.png",
            "memory_hpng/memory_test_1.png",
            "memory_hpng/science_bg.png",
            "memory_hpng/society_bg.png",
            "memory_hpng/speak_bg.png",
            "memory_hpng/square_bg.png",
            "memory_hpng/test_background.png",
            "memory_hpng/wel_background.png",
            "memory_hpng/cloud4.png"
    };

    /** ����Bitmapʱ�������߽������ţ���ֵ��ʾ���ż��� */
    private int memoryfraction;

    private boolean isLife;
    private int sleepTime;

    WodSurfaceView wod;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        init();
    }

    private void init() {
        isLife = true;
        sleepTime = SLEEP_TIME;
        memoryfraction = MEMORY_FRACTION;

        wod = new WodSurfaceView(SurfaceActivity.this);
        setContentView(wod);
    }

    @Override
    protected void onResume() {
//        call(MainActivity.class.getName());

        new Thread(wod).start();
        super.onResume();
    }

    @Override
    protected void execute(Bundle bund) {
//        new Thread(wod).start();
        super.execute(bund);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    class WodSurfaceView extends SurfaceView implements Callback, Runnable {

        private Context mContext;
        private ImageManager im;

        private SurfaceHolder surfaceHolder;

        private Paint paint;
        private Canvas canvas;

        private List<Bitmap> bitmapList;
        private ImageCacheManager icm;

        public WodSurfaceView(Context context) {
            super(context);

            mContext = context;

            init();
        }

        private void init() {
            bitmapList = new ArrayList<Bitmap>();

            im = new ImageManager(mContext);

            loadRes();

            /** ��ȡmSurfaceHolder **/
            surfaceHolder = getHolder();
            surfaceHolder.addCallback(this);
        }

        private void loadRes() {
            paint = new Paint();
        }

        void loadAssetsImageByOptions() {
            for (int i = 0; i < imgs.length; i++) {
                try {
                    Bitmap bitmap = im.createBitmap4AssetsByOption(imgs[i],
                            memoryfraction);
                    bitmapList.add(bitmap);

                    Log.i(TAG, "load " + imgs[i] + " and at " + i + " index.");
                } catch (IOException e) {

                }
            }
        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {

        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            isLife = false;
        }

        public void drawMe(Canvas canvas) {
//            Bitmap bitmap = bitmapList.get(0);
            Bitmap bitmap = icm.getBitmap("memory_hpng/test_background.png");
            Bitmap bitmap1 = icm.getBitmap("memory_hpng/artist_bg.png");

            Rect src = getBitmapRect(bitmap, 0, 0, 1);
            Rect dst = getBitmapRect(bitmap, 0, 0, memoryfraction);

            canvas.drawBitmap(bitmap, src, dst, paint);
            canvas.drawBitmap(bitmap1, 0, 0, paint);
        }

        @Override
        public void run() {
//            loadAssetsImageByOptions();
//            icm = im.createBitmapCache4AssetsByOptions(imgs, null);
            icm = im.createBitmapCache4AssetsByOptions(imgs, 1, 10, null);

            try {
                while (isLife) {
                    canvas = surfaceHolder.lockCanvas();

                    drawMe(canvas);

                    surfaceHolder.unlockCanvasAndPost(canvas);
                    Thread.sleep(sleepTime);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * @param bitmap
     *            ͼƬ
     * @param x
     *            x���
     * @param y
     *            y���
     * @param fraction
     *            bitmap���ŵı���
     * @return ����bitmap����Ļ������Ҫ���Ƶ�λ�ü���С
     */
    private Rect getBitmapRect(Bitmap bitmap, int x, int y, int fraction) {
        Bitmap bm = bitmap;
        Rect src = new Rect();

        int w = x + bm.getWidth() * fraction;
        int h = y + bm.getHeight() * fraction;

        bm = null;

        src.set(x, y, w, h);

//        Log.i(TAG, src.toShortString());
        return src;
    }
}
